﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Threading;

namespace Expressing_Label
{
    public partial class SQLCheckMiFrm : Form
    {
        private Point[] poin = new Point[8];
        private bool c_bMoving = false;
        private Point c_pStart = new Point();
        private int c_iProgressStatus = 0;
        private static Thread c_checkThread = null;
        private bool isFormClosing = false;
        private Bitmap c_bmpFormBackground = null;

        public SQLCheckMiFrm()
        {
            InitializeComponent();
            poin[0] = new Point(40, 0);
            poin[1] = new Point(Width, 0);
            poin[2] = new Point(Width, Height);
            poin[3] = new Point(40, Height);
            poin[4] = new Point(40, ConfigurationFrm.g_ySQLButton + 20);
            poin[5] = new Point(20, ConfigurationFrm.g_ySQLButton);
            poin[6] = new Point(40, ConfigurationFrm.g_ySQLButton - 20);
            poin[7] = new Point(40, 0);

            IntPtr hRgn = CreatePolygonRgn(ref poin[0], 16, 1);
            SetWindowRgn(Handle, hRgn, true);

            c_bmpFormBackground = new Bitmap(Width, Height);
            Graphics gra = Graphics.FromImage(c_bmpFormBackground);
            gra.Clear(Color.WhiteSmoke);
            Pen p = new Pen(Color.Purple, 4);
            for (int i = 0; i < 7; i++)
            {
                gra.DrawLine(p, poin[i], poin[i + 1]);
            }
            this.BackgroundImage = c_bmpFormBackground;
            gra.Dispose();

        }

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreatePolygonRgn(ref Point lpPoint, int nCount, int nPolyFillMode);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bReDraw);

        private void SQLCheckMiFrm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 4);
            for (int i = 0; i < 7; i++)
            {
                g.DrawLine(p, poin[i], poin[i + 1]);
            }
        }

        private void SQLCheckMiFrm_Load(object sender, EventArgs e)
        {
            this.Left = this.Owner.Left + this.Owner.Width - 40;
            this.Top = this.Owner.Top;
            DelayCheck_tmr.Start();
            CheckResult_lbl.Text = "Result";
            
        }

        private void SQLCheckMiFrm_MouseDown(object sender, MouseEventArgs e)
        {
            c_bMoving = true;
            c_pStart = MousePosition;
        }

        private void SQLCheckMiFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if (c_bMoving)
            {
                int x = MousePosition.X - c_pStart.X;
                int y = MousePosition.Y - c_pStart.Y;
                this.Left += x;
                this.Top += y;
                this.Owner.Left += x;
                this.Owner.Top += y;
                c_pStart = MousePosition;
            }
        }

        private void SQLCheckMiFrm_MouseUp(object sender, MouseEventArgs e)
        {
            c_bMoving = false;
        }

        private void SQLCheckMiFrm_MouseLeave(object sender, EventArgs e)
        {
            c_bMoving = false;
        }


        private void SQLCheck_Mtd()
        {
            
            foreach (var item in PublicResources.g_strConnection)
            {
                SqlConnection sqlcon = new SqlConnection();
                sqlcon.ConnectionString = PublicResources.g_strConnectionCommon;
                PublicResources.g_strCurrentConnection = item;
                if (isFormClosing)
                    return ;
                MethodInvoker mi = new MethodInvoker(this.CheckingMessage_Mtd);
                this.BeginInvoke(mi);
                try
                {
                    sqlcon.Open();
                }
                catch (Exception ex)
                {
                    if (isFormClosing)
                        return;
                    MethodInvoker fm = new MethodInvoker(this.CheckFailed_Mtd);
                    this.BeginInvoke(fm);
                }

                if (sqlcon.State == ConnectionState.Open)
                {
                    if (isFormClosing)
                        return;
                    MethodInvoker sm = new MethodInvoker(this.CheckSuccess_Mtd);
                    this.BeginInvoke(sm);
                    break;
                }
                sqlcon.Dispose();
                
            }
        }

        private void DelayCheck_tmr_Tick(object sender, EventArgs e)
        {
            DelayCheck_tmr.Stop();
            c_checkThread = new Thread(SQLCheck_Mtd);
            c_checkThread.IsBackground = true;
            c_checkThread.Start();
        }

        private void CheckingMessage_Mtd()
        {
            SQLName_lbl.Text = PublicResources.g_strCurrentConnection;
            CheckResult_lbl.Text = "Connecting" ;
            Checking_tmr.Start();
            Check_probr.Value = 0;
        }


        private void CheckFailed_Mtd()
        {
            CheckResult_lbl.Text = "FAIL";
            c_iProgressStatus = 0;
            Check_probr.Value = 0;
            Checking_tmr.Stop();
        }

        private void CheckSuccess_Mtd()
        {
            CheckResult_lbl.Text = "SUCCESS";
            c_iProgressStatus = 0;
            Check_probr.Value = 0;
            Checking_tmr.Stop();
        }
        
        private void Checking_tmr_Tick(object sender, EventArgs e)
        {
            if (c_iProgressStatus < 100)
            {
                c_iProgressStatus += 2;
                Check_probr.Value += 2;
            }

        }

        private void SQLName_lbl_SizeChanged(object sender, EventArgs e)
        {
            int x = this.Width - SQLName_lbl.Width;
            SQLName_lbl.Left = x / 2;
        }

        private void CheckResult_lbl_SizeChanged(object sender, EventArgs e)
        {
            int x = this.Width - CheckResult_lbl.Width;
            CheckResult_lbl.Left = x / 2;
        }

        private void SQLCheckMiFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            isFormClosing = true;
            if (c_checkThread != null)
                c_checkThread.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (CheckResult_lbl.Text == "SUCCESS")
            //{
            ConfigurationFrm.g_blSendByMiFrm = true;
            ConfigurationFrm.g_iNextFrm = 1;
            if(PublicResources.g_iConfiguraState == 0)
                PublicResources.g_iConfiguraState = 1;
            this.Close();
            //}
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }

    }
}
